//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Membership
{
	/// <summary>
	/// Summary description for ProfileOptions.
	/// </summary>
	public class ProfileOptions : BaseMembershipPage
	{


		#region Members

		protected YesNoRadioButtonList optAllowSignatures;
		protected YesNoRadioButtonList optEnableSignatures;
		protected TextBox txtSignatureMaxLength;
		protected YesNoRadioButtonList optAllowGender;
		protected YesNoRadioButtonList optAllowUserToSelectTheme;
		protected YesNoRadioButtonList optEnableUserReferrer;
		protected YesNoRadioButtonList optRequireAuthenticationForProfileViewing;

		protected StatusMessage formStatus;
		protected ResourceLinkButton btnSave;

		#endregion


		override protected void OnInit(EventArgs e)
		{
			btnSave.Click += new EventHandler(Save_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.DataBind();
			}

		}

		public override void DataBind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
    
			formStatus.Visible = false;
			optAllowSignatures.SelectedValue = settings.AllowUserSignatures;
			optEnableSignatures.SelectedValue = settings.EnableUserSignatures;
			txtSignatureMaxLength.Text = settings.UserSignatureMaxLength.ToString();
			optAllowGender.SelectedValue = settings.AllowGender;
			optAllowUserToSelectTheme.SelectedValue = settings.EnableUserThemeSelection;
			optRequireAuthenticationForProfileViewing.SelectedValue = settings.RequireAuthenticationForProfiles;

		}
	
		private void Save_Click(Object sender, EventArgs e) 
		{

			if (!Page.IsValid)
				return;
			
			SaveSettings();

			formStatus.Visible = true;
			formStatus.Success = true;
			formStatus.Text = string.Format(ResourceManager.GetString("CP_Membership_Cache_Warning"), CSContext.Current.SiteSettings.SiteSettingsCacheWindowInMinutes).Replace("'", @"\'");
		}

		protected virtual void SaveSettings() 
		{
			SiteSettings settings = CSContext.Current.SiteSettings; 

			settings.AllowUserSignatures = optAllowSignatures.SelectedValue;
			settings.EnableUserSignatures = optEnableSignatures.SelectedValue;
			settings.UserSignatureMaxLength = Int32.Parse( txtSignatureMaxLength.Text );
			settings.AllowGender = optAllowGender.SelectedValue;
			settings.EnableUserThemeSelection = optAllowUserToSelectTheme.SelectedValue;
			settings.RequireAuthenticationForProfiles = optRequireAuthenticationForProfileViewing.SelectedValue;

			SiteSettingsManager.Save(settings);
			
		}

	}
}
